/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_02_Properties.java]
 * Creation Date        [24-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_miscellaneous;

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_02_Properties {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				PropertiesFrame frame = new PropertiesFrame();
				frame.setVisible(true);
			}
		});
	}

}

/**
 * 根据属性文件的信息生成窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class PropertiesFrame extends JFrame {

	private static final long serialVersionUID = 3300726281353088487L;

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private File propertiesFile = null;
	private Properties settings = null;

	/**
	 * 构造方法
	 */
	public PropertiesFrame() {
		String userHome = System.getProperty("user.home");
		File propertiesFolder = new File(userHome, "core_java");
		if (!propertiesFolder.exists()) {
			propertiesFolder.mkdir();
		}

		// 从属性文件加载设置
		propertiesFile = new File(propertiesFolder, "program.properties");
		settings = new Properties();
		if (propertiesFile.exists()) {
			try {
				FileInputStream in = new FileInputStream(propertiesFile);
				settings.load(in);
				int left = Integer.parseInt(settings.getProperty("left"));
				int top = Integer.parseInt(settings.getProperty("top"));
				int width = Integer.parseInt(settings.getProperty("width"));
				int height = Integer.parseInt(settings.getProperty("height"));
				this.setBounds(left, top, width, height);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			this.setBounds(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
		}

		String title = settings.getProperty("title");

		if (title == null || title.equals("")) {
			title = JOptionPane.showInputDialog("Please input a frame title");
		}
		setTitle(title);

		addWindowListener(new WindowAdapter() {

			/**
			 * 在窗口关闭时将属性保存到配置文件
			 * 
			 * @param event
			 */
			@Override
			public void windowClosing(WindowEvent event) {
				settings.put("left", "" + getX());
				settings.put("top", "" + getY());
				settings.put("width", "" + getWidth());
				settings.put("height", "" + getHeight());
				settings.put("title", getTitle());

				try {
					FileOutputStream out = new FileOutputStream(propertiesFile);
					settings.store(out, "program properties");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.exit(0);
			}
		});
	}
}